/**  
 * Project Name:api-webhook  
 * File Name:ChsiService.java  
 * Package Name:com.example.service.webhook.business.chsi.service  
 * Date:2016年7月26日下午4:07:19  
 * Copyright (c) 2016, yuandong@51dojo.com All Rights Reserved.  
 *  
*/  
  
package com.example.service.webhook.business.chsi.service;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import com.example.service.webhook.business.chsi.api.ChsiClient;
import com.example.service.webhook.business.chsi.billitem.ChsiBillTask;
import com.example.service.webhook.business.chsi.dto.EducationInfo;
import com.example.service.webhook.business.chsi.dto.EducationInfoDetail;
import com.example.service.webhook.business.chsi.dto.StudentInfo;
import com.example.service.webhook.business.chsi.dto.StudentInfoDetail;
import com.example.service.webhook.business.chsi.entity.EducationInfoEntity;
import com.example.service.webhook.business.chsi.entity.StudentInfoEntity;
import com.example.service.webhook.business.chsi.repository.EducationRepository;
import com.example.service.webhook.business.chsi.repository.StudentRepository;
import com.example.service.webhook.business.fund.dto.FundDetails;

/**  
 * ClassName:ChsiService <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2016年7月26日 下午4:07:19 <br/>  
 * @author   yuandong  
 * @version    
 * @since    JDK 1.6  
 * @see        
 */
@Service
public class ChsiService {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ChsiService.class);
	
	
	@Autowired
	private ThreadPoolTaskExecutor taskExecutor;
	@Autowired
	private ChsiClient chsiClient;
	@Autowired
	private EducationRepository educationRepository;
	@Autowired
	private StudentRepository studentRepository;
	
	/**
	 * 
	 * fetchBill:处理学信网信息 <br/>
	 * 在收到魔蝎的邮箱账单回调时执行<br/>
	 * 学籍信息<br/>
	 * 学历信息<br/>
	 * 
	 * @author yuandong
	 * @param task
	 * @since JDK 1.6
	 */
	public void fetchBill(final ChsiBillTask  task) {
		// 这里交给线程池处理，防止下面的业务处理时间太长，导致超时。
		// 超时会导致魔蝎数据进行重试，会收到重复的回调请求
		taskExecutor.execute(
				new Runnable() {
                    @Override
                    public void run() {

            			try {
            				//保存学历信息
            				saveEducation(task);
            				//保存学籍信息
            				saveStudent(task);
            				
            			} catch (Exception e) {
            				LOGGER.error("fetchBill failed. taskId:{}", task.getTaskId(), e);
            			}
            		
                    	
                    }
                }
				
				);
		
		
	}
	
	
	private void saveEducation(ChsiBillTask task){
		try{
			EducationInfoDetail educationInfoDetail = chsiClient.getEducation(task.getTaskId());
			if(educationInfoDetail!=null&&!educationInfoDetail.getEducationList().isEmpty()){
				for(EducationInfo educationInfo:educationInfoDetail.getEducationList()){
					EducationInfoEntity isExist = educationRepository.getEducationInfo(task.getUserId(),educationInfo.getCertificateNo());
					EducationInfoEntity educationInfoEntity = new EducationInfoEntity();
					BeanUtils.copyProperties(educationInfo, educationInfoEntity);
					educationInfoEntity.setUserId(task.getUserId());
					if(isExist!=null){
						educationInfoEntity.setId(isExist.getId());
						educationInfoEntity.setCreateTime(isExist.getCreateTime());
						educationRepository.updateEducationInfo(educationInfoEntity);
					}else{
						educationRepository.saveEducationInfo(educationInfoEntity);
					}
				}
			}
		}catch(Exception e){
			LOGGER.error("saveEducation failed. taskId:{}", task.getTaskId(), e);
		}
	}

	private void saveStudent(ChsiBillTask task){
		try{
			StudentInfoDetail studentInfoDetail = chsiClient.getStudent(task.getTaskId());
			if(studentInfoDetail!=null&&!studentInfoDetail.getStudentInfoList().isEmpty()){
				for(StudentInfo studentInfo:studentInfoDetail.getStudentInfoList()){
					StudentInfoEntity isExist = studentRepository.getStudentInfo(task.getUserId(),studentInfo.getExamineeNo());
					StudentInfoEntity studentInfoEntity = new StudentInfoEntity();
					BeanUtils.copyProperties(studentInfo, studentInfoEntity);
					studentInfoEntity.setUserId(task.getUserId());
					if(isExist!=null){
						studentInfoEntity.setId(isExist.getId());
						studentInfoEntity.setCreateTime(isExist.getCreateTime());
						studentRepository.updateStudentInfo(studentInfoEntity);
					}else{
						studentRepository.saveStudentInfo(studentInfoEntity);
					}
				}
			}
		}catch(Exception e){
			LOGGER.error("saveStudent failed. taskId:{}", task.getTaskId(), e);
		}
	}
	
	
	
}
  
